/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using DataModelException = System.Qizx.Api.DataModelException;
using EvaluationException = System.Qizx.Api.EvaluationException;
using Node = System.Qizx.Api.Node;
using XMLUtil = System.Qizx.Util.Basic.XMLUtil;
using BasicNode = System.Qizx.Xdm.BasicNode;
using CoreDataModel = System.Qizx.Xdm.CoreDataModel;
using XMLPushStreamBase = System.Qizx.Xdm.XMLPushStreamBase;
using XdmNode = System.Qizx.Xdm.XdmNode;
using EvalContext = System.Qizx.XQuery.EvalContext;
using ExprDisplay = System.Qizx.XQuery.ExprDisplay;
using Focus = System.Qizx.XQuery.Focus;
using XQType = System.Qizx.XQuery.XQType;
using XQValue = System.Qizx.XQuery.XQValue;
namespace System.Qizx.XQuery.Op
{
	
	/// <summary> Implementation of constructors for TEXT, COMMENT.</summary>
	public class AtomConstructor:NodeConstructor
	{
        public System.Qizx.Api.XdmNodeType kind;

        public AtomConstructor(System.Qizx.Api.XdmNodeType kind, Expression contents)
		{
			this.kind = kind;
			this.contents = (contents == null)?new Expression[0]:new Expression[]{contents};
			type = XQType.TEXT;
			if (kind == System.Qizx.Api.XdmNodeType.Comment)
				type = XQType.COMMENT;
		}
		
		public override void  Dump(ExprDisplay d)
		{
			d.header(this);
			d.property("kind", "" + kind);
			d.children(contents);
		}
		
		public override BasicNode EvalAsNode(Focus focus, EvalContext context)
		{
			CoreDataModel dm = new CoreDataModel(null);
			XdmNode node;
			if (kind == System.Qizx.Api.XdmNodeType.Text)
				node = dm.newTextNode();
			else if (kind == System.Qizx.Api.XdmNodeType.Comment)
				node = dm.newCommentNode("");
			else
				throw new System.SystemException("bad AtomConstructor");
			
			bool empty = true;
			for (int c = 0; c < contents.Length; c++)
			{
				XQValue cont = contents[c].eval(focus, context);
				for (; cont.Next(); )
				{
					if (!empty)
						node.AddText(" ");
					empty = false;
					node.AddText(cont.ValueAsString);
				}
			}
			if (empty)
				return null;
			if (kind == System.Qizx.Api.XdmNodeType.Comment)
			{
				try
				{
					string contents = node.StringValue;
					if (!XMLUtil.checkComment(contents))
						context.error("XQDY0072", this, "invalid comment");
				}
				catch (DataModelException e)
				{
					dmError(context, e);
				}
			}
			return node;
		}
		
		public override void  EvalAsEvents(XMLPushStreamBase output, Focus focus, EvalContext context)
		{
			context.at(this);
			
			try
			{
				if (kind == System.Qizx.Api.XdmNodeType.Text)
				{
					for (int c = 0; c < contents.Length; c++)
					{
						XQValue cont = contents[c].eval(focus, context);
						for (; cont.Next(); )
							output.putAtomText(cont.ValueAsString);
					}
					return ;
				}
				
				System.Text.StringBuilder buf = new System.Text.StringBuilder();
				for (int c = 0; c < contents.Length; c++)
				{
					XQValue cont = contents[c].eval(focus, context);
					for (; cont.Next(); )
						buf.Append(cont.ValueAsString);
				}
				string chunk = buf.ToString();
				if (kind == System.Qizx.Api.XdmNodeType.Comment)
					output.putComment(chunk);
				else
					throw new System.SystemException("bad AtomConstructor");
			}
			catch (DataModelException e)
			{
				dmError(context, e);
			}
		}
	}
}